﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AutoPathFinding : MonoBehaviour {
    [SerializeField]
    List<Vector3> path = new List<Vector3>();
    [SerializeField]
    bool loop = true;
    [SerializeField]
    float moveSpeed = 1f;

    Transform cacheTran;
    int curIndex = 0;

	// Use this for initialization
	void Start () {
        cacheTran = transform;

    }
	
	// Update is called once per frame
	void Update () {
        if(path.Count == 0)
        {
            enabled = false;
            return;
        }

        if(Vector3.Distance(cacheTran.localPosition,path[curIndex]) <= 0.01f)
        {
            curIndex ++;
            if (curIndex >= path.Count)
            {
                if (loop == true)
                {
                    curIndex = 0;
                }
                else
                {
                    curIndex = path.Count - 1;
                    enabled = false;
                    return;
                }
            }
            cacheTran.localPosition = Vector3.MoveTowards(cacheTran.localPosition, path[curIndex], moveSpeed * Time.deltaTime);
        }
        else
        {
            cacheTran.localPosition = Vector3.MoveTowards(cacheTran.localPosition, path[curIndex], moveSpeed * Time.deltaTime);
        }
    }
}
